/**
 * 作者: wgx
 * 日期: 2025-05-06 15:19:25
 * 备注: 用户大世界行军模块数据
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.commondata.bag.ItemId;
import com.motu.monstercity.server.game.commondata.maintask.MainTaskConstant;
import com.motu.monstercity.server.game.log.LogType;
import com.motu.monstercity.server.game.log.MotuLogManager;
import com.motu.monstercity.server.game.log.ThinkingDataManager;
import com.motu.monstercity.server.game.logic.ChildManager;
import com.motu.monstercity.server.game.logic.GameDataManager;
import com.motu.monstercity.server.game.logic.GameUser;
import com.motu.monstercity.server.game.logic.MainTaskManager;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import io.vertx.core.json.JsonObject;

public class UserWorldInfo extends UserWorldInfoModel {
    public UserWorldInfo() {
        super();
    }

    public JsonObject insert() {
        putEnergy(AllParam.WORLD_ENERGY_LIMIT);
        putEnergyTime(TimeUtils.getCurTime());
        putResetTime(TimeUtils.getCurTime());
        putUrgentRefreshNum(0);
        putEnergyBuyNum(0);
        super.putMonsterLevel(1);// 怪兽初始等级1级
        return super.insert();
    }

    public void putMonsterLevel(UserInfo userInfo, int newLevel) {
        if (this.monsterLevel != newLevel) {
            if (newLevel < getMonsterLevel()) {// 降级时  最小1级，不能低于历史最高级-5
                if (newLevel < getMonsterLevelMax() - AllParam.WORLD_MONSTER_LEVEL_LIMIT_MIN) {
                    newLevel = getMonsterLevelMax() - AllParam.WORLD_MONSTER_LEVEL_LIMIT_MIN;
                }
                if (newLevel < 1) {
                    newLevel = 1;
                }
            }
            super.putMonsterLevel(newLevel);
            if (getMonsterLevel() > getMonsterLevelMax()) {
                putMonsterLevelMax(getMonsterLevel());// 更新历史最高等级
                ThinkingDataManager.pushUserWorldMonsterLeveUpEvent(userInfo, newLevel);// 数数上报数据
            }
        }
    }

    public void reset() {
        boolean isUpdate = false;
        if (!TimeUtils.isToday(getResetTime())) {
            putUrgentRefreshNum(0);
            putEnergyBuyNum(0);
            putResetTime(TimeUtils.getCurTime());
            isUpdate = true;
        }

        if (recoverEnergy()) {
            isUpdate = true;
        }

        if (isUpdate) {
            update();
        }
    }

    public boolean recoverEnergy() {
        int max = AllParam.WORLD_ENERGY_LIMIT;// 体力上限
        if (getEnergy() >= max) {// 此时体力已经达到上限
            return false;
        }
        int cdTime = AllParam.WORLD_ENERGY_RECOVER_TIME;// 体力恢复的CD时间
        long diff = TimeUtils.getTimeDiff(getEnergyTime());
        if (max == 0 || cdTime == 0 || diff < cdTime) {
            return false;// 不需要执行恢复逻辑
        }

        int addNum = (int) Math.floor((double) diff / cdTime);  // 恢复体力点数
        int balance = getEnergy();// 当前体力余额
        if (balance + addNum < max) {
            putEnergyTime(getEnergyTime() + cdTime * addNum);// // 恢复了n点, 标记时间就加N个cdtime
        } else {
            addNum = max - balance;
            putEnergyTime(TimeUtils.getCurTime());
        }
        UserInfo userInfo = GameUser.getUserInfo(getId());
        addEnergy(userInfo, addNum, LogType.SYSTEM_REWARD);
        return true;
    }

    // 记录日志并增加体力
    public void addEnergy(UserInfo userInfo, int num, Object... params) {
        int balance = getEnergy();// 当前余额
        long dbid = 0;
        MotuLogManager.logItemIncome(userInfo, ItemId.TYPE_WORLD_ENERGY, dbid, balance, num, params);
        super.addEnergy(num);
    }

    // 扣除体力
    public void subEnergy(UserInfo userInfo, int value, Object... params) {
        int balance = getEnergy();
        if (value > balance) {
            value = balance;
        }
        int max = AllParam.WORLD_ENERGY_LIMIT;// 体力上限
        if (balance >= max && balance - value < max) {// 原来的体力超过上限,且使用后低于下限,开始cd倒计
            putEnergyTime(TimeUtils.getCurTime());
        }
        long dbid = 0;
        MotuLogManager.logItemConsume(userInfo, ItemId.TYPE_WORLD_ENERGY, dbid, balance, value, params);
        super.addEnergy(-value);
    }

}
